//
//  EvaluateViewController.m
//  ClassRoom
//
//  Created by apple on 12-12-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "EvaluateViewController.h"
#import "UrlData.h"
#import "LoginStatus.h"
@interface EvaluateViewController ()
- (void)setUpCourseEvaluateView;
- (void)setUpEnvironMentEvaluateView;
- (void)setUpTeachingEvaluateView;
- (void)setUpBackGround;

@end
BOOL hasComprehensiveRateView=FALSE;

@implementation EvaluateViewController
@synthesize evaluatePoint;
@synthesize courseInfoDic;
@synthesize courseRate;
@synthesize environmentRate;
@synthesize teachingtRate;
@synthesize comprehensiveRate;
@synthesize descriptionTextView;
@synthesize comprehensiveRateView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"课程评价";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   // self.evaluatePoint=-1;
    self.descriptionTextView.returnKeyType=UIReturnKeyDone;
    self.descriptionTextView.delegate=self;
    comprehensiveRate=0;
    [self setUpBackGround];
    
    [self setUpCourseEvaluateView];
    [self setUpEnvironMentEvaluateView];
    [self setUpTeachingEvaluateView];
    [self setUpComprehensiveRateView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)setUpBackGround
{
    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"Evaluate_Background.png"]];
}
- (void)setUpCourseEvaluateView {
    
    DYRateView* courseRateView = [[DYRateView alloc] initWithFrame:CGRectMake(140, 122-20, 100, 24) fullStar:[UIImage imageNamed:@"Evaluate_StarFull.png"] emptyStar:[UIImage imageNamed:@"Evaluate_StarEmpty.png"]];
    courseRateView.padding = 4;
    courseRateView.tag=1;
    //rateView.alignment = RateViewAlignmentCenter;
    courseRateView.alignment=RateViewAlignmentLeft;
    courseRateView.editable = YES;
    courseRateView.delegate = self;
    [self.view addSubview:courseRateView];
    [courseRateView release];
    
}
- (void)setUpEnvironMentEvaluateView {
    DYRateView* environMentRateView = [[DYRateView alloc] initWithFrame:CGRectMake(140, 147-20, 100, 24) fullStar:[UIImage imageNamed:@"Evaluate_StarFull.png"] emptyStar:[UIImage imageNamed:@"Evaluate_StarEmpty.png"]];
    environMentRateView.tag=2;
    environMentRateView.padding = 4;
    //rateView.alignment = RateViewAlignmentCenter;
    environMentRateView.alignment=RateViewAlignmentLeft;
    environMentRateView.editable = YES;
    environMentRateView.delegate = self;
    [self.view addSubview:environMentRateView];
    [environMentRateView release];
    
}
- (void)setUpTeachingEvaluateView {
    DYRateView* teachingRateView = [[DYRateView alloc] initWithFrame:CGRectMake(140, 172-20, 100, 24) fullStar:[UIImage imageNamed:@"Evaluate_StarFull.png"] emptyStar:[UIImage imageNamed:@"Evaluate_StarEmpty.png"]];
    teachingRateView.padding = 4;
    teachingRateView.tag=3;
    //teachingRateView.isLargeStar= FALSE;
    //rateView.alignment = RateViewAlignmentCenter;
    teachingRateView.alignment=RateViewAlignmentLeft;
    teachingRateView.editable = YES;
    teachingRateView.delegate = self;
    [self.view addSubview:teachingRateView];
    [teachingRateView release];
    
}


 
 

- (void)setUpComprehensiveRateView {
   
    [comprehensiveRateView removeFromSuperview];
    
    comprehensiveRateView=[[[DYRateView alloc] initWithFrame:CGRectMake(150, 56-22, 140, 50)fullStar:[UIImage imageNamed:@"Evaluate_StarFullLarge.png"] emptyStar:[UIImage imageNamed:@"Evaluate_StarEmptyLarge3.png"]] autorelease];
    comprehensiveRateView.rate = comprehensiveRate;
    comprehensiveRateView.alignment = RateViewAlignmentCenter;
    [comprehensiveRateView setNeedsDisplay];
    [self.view addSubview:comprehensiveRateView];

    }

   
       

-(NSString *)createUrlStr
{
    NSLog(@"vvvvvvvvcourseInfoDic=%@",courseInfoDic);
  
    NSString *urlString=@"";
    NSLog(@"vvvvvcomprehensiveRat%f",comprehensiveRate);
    
    
    //NSString *descriptionStr=descriptionTextView.text;
    NSString *descriptionStr=[self encodeURL:descriptionTextView.text];
   // NSLog(@"descriptionStr=%@",descriptionStr);
    [descriptionStr retain];
    urlString=[NSString stringWithFormat:@"%@%@userID=%@&courseID=%@&grade=%0.2f&description=%@",ClassRoom_URL,GradeUserCourse_URL,[LoginStatus LoginedUserID].userID,(NSString *)[courseInfoDic objectForKey:@"courseId"],comprehensiveRate,descriptionStr];
 
    NSLog(@"urlString=%@",urlString);
    return urlString;

}

- (IBAction)commitButtonPressed:(UIButton *)sender {
    [self createUrlStr];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)setKeyBoardDown:(id)sender{
   [descriptionTextView resignFirstResponder];
    NSTimeInterval animationDuration = 0.30f;       
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0, 20, 320, 460);
    self.view.frame = rect;
}

-(IBAction)backgroundTap:(id)sender//点击背景
{
    //[  resignFirstResponder];
    [descriptionTextView resignFirstResponder];
    NSTimeInterval animationDuration = 0.30f;       
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       
    [UIView setAnimationDuration:animationDuration];
   // CGRect rect = CGRectMake(0, 20, 320, 460);
  //  self.view.frame = rect;
}


#pragma mark - encoding
-(NSString*) encodeURL:(NSString*) unescapedString
{
    NSString* escapedUrlString= (NSString*) CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)unescapedString, NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
    escapedUrlString = [escapedUrlString stringByAddingPercentEscapesUsingEncoding:kCFStringEncodingUTF8];
    return [escapedUrlString autorelease];
}


#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {    
    
    UIBarButtonItem *done =    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];    
    
    self.navigationItem.rightBarButtonItem = done;   
   
    
    //set frame
    CGRect frame = descriptionTextView.frame;
    
    int offset = frame.origin.y + 32+125 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;               
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];               
    
    [UIView setAnimationDuration:animationDuration];
    
    float width = self.view.frame.size.width;               
    
    float height = self.view.frame.size.height;       
    
    if(offset > 0)
        
    {
        
        CGRect rect = CGRectMake(0.0f, -offset,width,height);               
        
        self.view.frame = rect;       
        
    }       
    
    [UIView commitAnimations];      

    
}    

- (void)textViewDidEndEditing:(UITextView *)textView {    
    
    self.navigationItem.rightBarButtonItem = nil;  
    
    
    //set frame
    NSTimeInterval animationDuration = 0.30f;       
    
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       
    
    [UIView setAnimationDuration:animationDuration];       
    
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);  
    
    //CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
    
    self.view.frame = rect;
    
    [UIView commitAnimations];
    
    [descriptionTextView resignFirstResponder];
    

    
}    

- (void)leaveEditMode {    
    
    [self.descriptionTextView resignFirstResponder];  
    
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text 
    { 
        if ([text isEqualToString:@"\n"]) { 
            [textView resignFirstResponder];  
            return NO; 
        } 
        return YES; 
    } 
    
   
#pragma mark - DYRateViewDelegate

- (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)rate 
{
    if(rateView.tag==1)
    {
        courseRate= rate.intValue;
    }else if(rateView.tag==2){
        environmentRate=rate.intValue;
    }else if(rateView.tag==3){
        teachingtRate= rate.intValue;
    }
    comprehensiveRate=((courseRate+environmentRate+teachingtRate)/3.0);
//    NSString* comprehensiveRateStr= [NSString stringWithFormat:@"%0.2f",comprehensiveRate];
//    comprehensiveRate=[comprehensiveRateStr floatValue];
//    NSLog(@"comprehensiveRate=%f",comprehensiveRate);
   // [comprehensiveRateStr retain];
    [self setUpComprehensiveRateView];
    
}
 
@end
